package com.etsoft.comm.tool;

import com.etsoft.comm.db.DBHelper;
import com.etsoft.comm.db.Conf;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class GContext {
    public static String sampleFilePath = System.getProperty("user.dir") + "/generator/target/classes/";// + "/sample/SampleDao.java";
    // public static String sampleFilePath = System.getProperty("java.class.path").split(";")[0];

    private final String tablePrefixName = Conf.getByKey("TablePrefixName");
    public final String project_name = Conf.getByKey("project_name");
    public final String deleteFlagColumn = Conf.getByKey("DeleteFlagColumn");
    public static List<String> defaultList = new ArrayList<String>() {{
        add("id");
        // add("create_user");
        // add("create_time");
        // add("update_user");
        // add("update_time");
        add("del_flag");
    }};

    Connection l_conn = DBHelper.getConnection();

    private String table_name;
    public String filePath;
    private ResultSet l_rset;

    public GContext(String table_name) throws Exception {
        this.table_name = table_name;
        this.filePath = Conf.getByKey("codeFilePath");
        this.l_rset = l_conn.prepareStatement("select * from " + table_name + " limit 1").executeQuery();
    }

    public ResultSet getL_rset() {
        return l_rset;
    }
    public String getTable_name() {
        return table_name;
    }

    public String getClassName(boolean isFirstUpperCase) {
        return StringUnit.getCamelName(table_name, tablePrefixName, isFirstUpperCase);
    }

    public String getClassNameSuffix(SampleFile s) {
        return getClassName(true) + Conf.getByKey(s.toString() + "Suffix");
    }
    // String fileName = filePath + ClassName + Suffix + Ext;
    public String getFileFullPath(SampleFile s){
        String fullPath = filePath + Conf.getByKey(s.toString() + "Folder") + File.separator;
        FileHelper.checkDirectoryExsit(fullPath);
        return fullPath + getClassNameSuffix(s) + Conf.getByKey(s.toString() + "FileExt");
    }

    public String getSampleStr(SampleFile f) throws Exception {
        String file_path = Conf.getByKey(f.toString() + "Sample");

        return FileHelper.readToString(sampleFilePath + file_path);
    }

    public boolean is_date_exist() throws SQLException {
        boolean l_date = false;// 有无时间类型
        for (int i = 1; i <= l_rset.getMetaData().getColumnCount(); i++) {
            String col_name = l_rset.getMetaData().getColumnName(i).toLowerCase();
            if (defaultList.contains(col_name)) {
                continue; // 这些封装在父类里了
            }
            if (TypeHelper.isDateType(l_rset.getMetaData(), i)) {
                l_date = true; break;
            }
        }
        return l_date;
    }

    public boolean is_deleted_exist() throws SQLException {
        for (int i = 1; i <= l_rset.getMetaData().getColumnCount(); i++) {
            String col_name = l_rset.getMetaData().getColumnName(i).toLowerCase();
            if (deleteFlagColumn.equalsIgnoreCase(col_name)) {
                return true;
            }
        }
        return false;
    }

    public String getPK() throws Exception {
        String pkField = "";// 默认主键只有一个
        // 主键
        ResultSet l_column_key = l_conn.getMetaData().getPrimaryKeys(null, null, getTable_name());
        while (l_column_key.next()) {
            for (int i = 1; i <= l_column_key.getMetaData().getColumnCount(); i++) {
                if (!StringUnit.isNullOrEmpty(l_column_key.getString("column_name"))) {
                    pkField = l_column_key.getString("column_name").toLowerCase();
                    break;
                }
            }
        }
        if(StringUnit.isNullOrEmpty(pkField)) {
            throw new Exception(table_name + " 表没有主键");
        }

        return pkField;
    }

    public List<DbField> getDbFieldList() throws Exception {
        List<DbField> list = new LinkedList<>();
        for (int i = 1; i <= l_rset.getMetaData().getColumnCount(); i++) {
            String type = TypeHelper.getTypeFromDb2Java(l_rset.getMetaData(), i);
            String col_name = l_rset.getMetaData().getColumnName(i).toLowerCase();
            list.add(new DbField(type, col_name));
        }
        return list;
    }

    public static class DbField {
        private String type;
        private String name;
        public DbField(String type, String name) {
            this.type = type;
            this.name = name;
        }
        public String getFName(boolean isFirstUpperCase) {
            return StringUnit.getCamelName(this.name, isFirstUpperCase);
        }
        public String getJdbcName() {
            return TypeHelper.getTypeFromJava2Jdbc(type);
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
